package com.tfswue.lsh.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tfswue.lsh.entity.User;
import com.tfswue.lsh.service.UserService;
import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class UserController {
    private Map<Long, User> userMap = new HashMap<>();
    private long nextId = 1;

    @Autowired
    private UserService userService;

    @RequestMapping("/getUser")
    public User getUser() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", 1);
        List<User> list = userService.list(queryWrapper);

        return userService.getById(1);
    }

    @GetMapping("/{id}")
    public User getUserById(@NonNull @PathVariable long id) {
        return userMap.get(id);
    }

    //创建用户
    @PostMapping
    public User create(@RequestBody User user) {
        user.setId((int) nextId++);
        userMap.put(Long.valueOf(user.getId()), user);
        return user;
    }

    //更新用户
    @PutMapping("/{id}")
    public User updateUser(@PathVariable long id, @RequestBody User userDetails) {
        User user = userMap.get(id);
        if (user != null) {
            user.setUsername(userDetails.getEmail());
        }

return  user;
    }
}
